package main;

import java.util.ArrayList;
import java.util.concurrent.Callable;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Parses web pages.
 * @version 5-11-2012
 * @author T. Lewis & Ken Norton BitWiseGuys.
 * */
public class Parser implements Callable<WebPage> {

	private ArrayList<String> my_urlList;
	private WebPage my_webpage;
	private int my_linkCount;
	private long StartParseTime;
	private String[] my_KeyWordList;


	public Parser(WebPage the_webpage, String[] the_KeyWordList){
		my_webpage = the_webpage;
		my_urlList = new ArrayList<String>();
		my_linkCount = 0;
		StartParseTime = System.nanoTime();
		my_KeyWordList = the_KeyWordList;
	}

	@Override
	public WebPage call() throws Exception {

		Elements links = my_webpage.getDoc().select("a[href]");

		for (Element link : links) {

			if (Validate.isValid(link.attr("abs:href"))) {
				Validate.visit(link.attr("abs:href"));
				my_urlList.add((link.attr("abs:href")));
			}
			my_linkCount ++;
		}
		my_webpage.setLinks(my_linkCount);
		my_webpage.set_ParseTime(StartParseTime);
		my_webpage.setLinkList(my_urlList);
		Spider.totalLinks += my_linkCount;
		return my_webpage;
	}
}